﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TMScore_Subsets
{
    class ReadFile
    {
         
        /// <summary>
        /// read all the files and insert all the fields to the tuple
        /// </summary>
        /// <returns></returns>
        public static Tuple<int, int, bool, bool, Dictionary<string, Tuple<string, List<string>, DateTime, double>>, int, int> ReadAllFile()
        {
            bool a=true;
            Dictionary<string, Tuple<string, List<string>, DateTime, double>> m_allDocs = new Dictionary<string, Tuple<string, List<string>, DateTime, double>>(); ; //Dictionary<fileName ,tuple <all text, list of recivers,date,TMscore>
            string path = Directory.GetCurrentDirectory(); // get the directory where the EXE/Application file is
            path += "\\Files\\Mails"; // add the folder name "Files" to the path
            string[] files = Directory.GetFiles(path); // get all files from the path
            bool isSteam = false;
            bool repeatNGrams = false;
            int k = 0;
            int n = 0;

            int tops = 0;
            int chosenDocsFromClusters = 0;

            foreach (string file in files) // for each of the files
            {
                a=true;
                string[] recievers = null;
                string [] time =null;
                string tempString = "";
                DateTime t;
                string temp = "";
                string[] splitLine = null;
                string[] splitLine2 = null;
                List<string> cleanLine = new List<string>();
                int j = 0;
                double TMscore=0;
                string fileName = file.Split('\\').Last();
                StreamReader sr = new StreamReader(file); // open a new StreamReader
                if (fileName == "stopWords.txt")//if file: "stopWords.txt"
                    continue;
                if (fileName == "config.txt")//if file: "config.txt", file structure: k,n,isStream?,doSplit?
                {
                    string line = sr.ReadLine(); // Read a line 
                    string[] split = line.Split(new char[] { ',' }); // split the line and get the Doc Number
                    if (split.Length != 7) // cheks if input data length in config file is correct
                    {
                        Console.WriteLine("Configuration file is lack of data - Programg will be terminated");
                        return null;
                    }
                    k = int.Parse(split[0]);
                    n = int.Parse(split[1]);
                    tops = int.Parse(split[4]);
                    chosenDocsFromClusters = int.Parse(split[5]);
                    if (split[2] == "YES")
                        isSteam = true;
                    if (split[3] == "YES")
                        repeatNGrams = true;
                }
                else
                {
                    string firstLine = sr.ReadLine(); // Read a line 
                    splitLine = firstLine.Split(new char[] { ',' }); // split the line and get the Doc Number
                    recievers = splitLine[1].Split(new char[] { ';' });
                    if (recievers[0] == "")
                        continue;

                    foreach (string i in recievers)
                    {
                        tempString = "";
    
                                if (i[0] == ' ')
                                {
                                    tempString = i.Substring(1);
                                    if (!cleanLine.Contains(tempString))
                                    cleanLine.Add(tempString);
                                }
                                else
                                      if (!cleanLine.Contains(i))
                                    cleanLine.Add(i);     
                    }
                    splitLine2 = fileName.Split(new char[] { '#' }); // split the line and get the Doc Number
                    temp = splitLine2[1];
                    temp = temp.Remove(temp.Length - 4);
                    time = temp.Split(new char[] { '_' });
                    t = new DateTime(Convert.ToInt32(time[2]), Convert.ToInt32(time[0]), Convert.ToInt32(time[1]));
                  
                    TMscore = Double.Parse(splitLine[2]);
                    List<string> rec = new List<string>(cleanLine);
                    Tuple<string, List<string>, DateTime, double> all = new Tuple<string, List<string>, DateTime, double>(sr.ReadToEnd(), rec, t, TMscore);
                    foreach (string m in m_allDocs.Keys)
                    {
                        if (fileName == m)
                            a = false;
                    }
                    if(a==true)
                    m_allDocs.Add(fileName, all); //adds all text from the file with key = Doc Name

                }
                sr.Close();
            }

          
            m_allDocs = m_allDocs.OrderBy(x => x.Value.Item3).ToDictionary(x => x.Key, x => x.Value);
            Tuple<int, int, bool, bool, Dictionary<string, Tuple<string, List<string>, DateTime, double>>, int, int> allItems = new Tuple<int, int, bool, bool, Dictionary<string, Tuple<string, List<string>, DateTime, double>>, int, int>(k, n, isSteam, repeatNGrams, m_allDocs, tops, chosenDocsFromClusters);
            return allItems;
        }

    }
}
